package JackSu.多线程;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 1.用callable方式能获取返回值
 * 2.可以自己抛出异常，被捕获
 * 3.支持泛型
 */

class myThread  implements Callable{
    @Override
    public Object call() throws Exception {
        int sum=0;
        for (int i = 0; i < 100; i++) {
            if (i%2==0){
                System.out.println(i);
                sum+=i;
            }
        }
        return sum;
    }
}


public class _08jdk5新增的callable方式 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {


        myThread myThread = new myThread();
        FutureTask myThreadFutureTask = new FutureTask(myThread);
        Thread thread = new Thread(myThreadFutureTask);
        thread.start();
    Object sum = myThreadFutureTask.get();
        System.out.println("从子线程中得到的返回值"+sum);


    }
}



